package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.OffLineLoadUnloadGroupTable;
import com.yimi.yinhepda.entity.bean.LoadUnloadGroupEntity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 离线装卸组操作类
 *
 * @author lj
 */
public class OffLineLoadUnloadGroupTableDao extends BaseDao<OffLineLoadUnloadGroupTable, Long> {
    public OffLineLoadUnloadGroupTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), OffLineLoadUnloadGroupTable.class);
    }

    public void createOrUpdate(String deptCode, String teamCodes, String teamNames, List<LoadUnloadGroupEntity> groupEntityList) {
        try {
            //删除数据
            List<OffLineLoadUnloadGroupTable> tableList = getDataByDeptCode(deptCode);
            if (tableList != null) {
                delete(tableList);
            }
            //插入数据
            if (groupEntityList != null) {
                List<OffLineLoadUnloadGroupTable> lists = new ArrayList<>();
                for (LoadUnloadGroupEntity entity : groupEntityList) {
                    OffLineLoadUnloadGroupTable groupTable = new OffLineLoadUnloadGroupTable();
                    groupTable.setTeamCodes(teamCodes);
                    groupTable.setTeamNames(teamNames);
                    groupTable.setLoaderteamLoadersId(entity.getLoaderteamLoadersId());
                    groupTable.setTeamCode(entity.getTeamCode());
                    groupTable.setTeamName(entity.getTeamName());
                    groupTable.setEmpCode(entity.getEmpCode());
                    groupTable.setEmpName(entity.getEmpName());
                    groupTable.setDeptCode(entity.getDeptCode());
                    groupTable.setDeptName(entity.getDeptName());
                    groupTable.setModifierTime(entity.getModifierTime());
                    groupTable.setCreater(entity.getCreater());
                    groupTable.setCreaterTime(entity.getCreaterTime());
                    groupTable.setIsDelete(entity.getIsDelete());
                    groupTable.setRecordVersion(entity.getRecordVersion());
                    groupTable.setIsLeader(entity.getIsLeader());
                    groupTable.setLatestTime(entity.getLatestTime());
                    groupTable.setUserType(entity.getUserType());
                    lists.add(groupTable);
                }
                insert(lists);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<OffLineLoadUnloadGroupTable> getDataByDeptCode(String deptCode) {
        try {
            QueryBuilder<OffLineLoadUnloadGroupTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("deptCode", deptCode);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据工号获取数据
     */
    public OffLineLoadUnloadGroupTable getDataByWorkNum(String workNum) {
        try {
            QueryBuilder<OffLineLoadUnloadGroupTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.like("empCode", "%" + workNum + "%");
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据姓名获取数据
     */
    public OffLineLoadUnloadGroupTable getDataByName(String empName) {
        try {
            QueryBuilder<OffLineLoadUnloadGroupTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.like("empName", empName);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
